package cn.darkfire.web.Filter;


import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/***
 * 过滤器,可指定过滤的页面的请求以及响应,
 * 如CORS 同源策略 设置允许跨域的域
 */
@WebFilter("/sy3/show.jsp")
public class RootFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest httpReq = (HttpServletRequest) servletRequest;
        HttpServletResponse httpRep = (HttpServletResponse) servletResponse;
        try{
            if((Integer) httpReq.getSession().getAttribute("login")!=1){
                httpReq.getSession().setAttribute("error","过滤器检测,未登录！");
                httpRep.sendRedirect("./error.jsp");
            }else{
                filterChain.doFilter(servletRequest,servletResponse);
            }
        }catch (Exception e){
            httpReq.getSession().setAttribute("error","过滤器检测,未登录！");
            httpRep.sendRedirect("./error.jsp");
        }

    }

    @Override
    public void destroy() {

    }
}
